package com.hz.house.service.home;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hz.house.bean.entity.BuildingUnitReviewBean;
import com.hz.house.bean.entity.BuildingUnitsSubmitBean;
import com.hz.house.bean.vo.UnitReviewInfo;
import com.hz.house.cache.HomePageCacheService;
import com.hz.house.common.ResultUtil;
import com.hz.house.dao.UnitReviewMapper;

/**
 * <p>楼盘评测业务层
 * @author lfw
 *
 */
@Service
public class BuildingUnitReviewService {

	@Autowired
	private UnitReviewMapper unitReviewMapper;
	
	@Autowired
	private HomePageCacheService homePageCacheService;
	
	public Object applyReview(UnitReviewInfo info,int customerId) {
		
		if(StringUtils.isEmpty(info.getBuildingName()))
			info.setBuildingName("未知");
		if(StringUtils.isEmpty(info.getNum()))
			info.setNum("未知");
		if(StringUtils.isEmpty(info.getSpace()))
			return ResultUtil.fail("建筑面积必须填写");
		if(StringUtils.isEmpty(info.getUnitImg()))
			return ResultUtil.fail("户型图必须上传");
		BuildingUnitsSubmitBean s = new BuildingUnitsSubmitBean();
		s.setArea(info.getSpace());
		s.setBuildingName(info.getBuildingName());
		s.setCreateTime(new Date());
		s.setCustomerId(customerId);
		s.setImg(info.getUnitImg());
		s.setPhone(info.getPhone());
		s.setUnitsNum(info.getNum());
		int x = this.unitReviewMapper.addReviewApply(s);
		if(x>0)
			return ResultUtil.ok();
		return ResultUtil.fail("提交失败");
	}
	
	/**
	 * 	获取评测列表或查询
	 * @param page
	 * @param keyword
	 * @return
	 */
	public Object getUnitReviewList(Integer page,String keyword) {
		
		if(page==null || page<1)
			page=1;
		Map<String,Object> m = new HashMap<>();
		m.put("page", (page-1)*15);
		m.put("size", 15);
		
		if(!StringUtils.isEmpty(keyword))
			 return this.handleSearch(page, keyword);
			 
		//如果不是搜索，则走缓存
		Integer total = this.homePageCacheService.getReviewListTotal();
		if(total==null) {
			total = this.unitReviewMapper.getListTotal(m);
			if(total==null || total==0) {
				m.clear();
				m.put("total", 0);
				m.put("rows",new ArrayList<>());
				this.homePageCacheService.setReviewListTotal(0);
				return ResultUtil.ok(m);
			}
			this.homePageCacheService.setReviewListTotal(total);
		}
		List<BuildingUnitReviewBean> ls = this.homePageCacheService.getReviewListPage(page);
		if(ls==null) {
			ls = this.unitReviewMapper.getList(m);
			this.homePageCacheService.setReviewListPage(ls, page);
		}
		ls.forEach(r->{
			 Float s = Float.valueOf(r.getScore());
			 r.setScore_(s/10f);
			 
			 r.setScore(null);
			 if(StringUtils.isEmpty(r.getUnitImg())) {
				 r.setUnitImg(r.getUnitImg_());
			 }
			 r.setUnitImg_(null);
		});
		m.clear();
		m.put("total",total);
		m.put("rows",ls);
		return ResultUtil.ok(m);
	}
	
	
	private Object handleSearch(Integer page,String keyword) {
		Map<String,Object> m = new HashMap<>();
		m.put("page", (page-1)*15);
		m.put("size", 15);
		m.put("key", keyword);
		Integer total = this.unitReviewMapper.getListTotal(m);
		if(total==null || total==0) {
			m.clear();
			m.put("total", 0);
			m.put("rows",new ArrayList<>());
			return ResultUtil.ok(m);
		}
		List<BuildingUnitReviewBean> ls = this.unitReviewMapper.getList(m);
		ls.forEach(r->{
			 Float s = Float.valueOf(r.getScore());
			 r.setScore_(s/10f);
			 
			 r.setScore(null);
			 if(StringUtils.isEmpty(r.getUnitImg())) {
				 r.setUnitImg(r.getUnitImg_());
			 }
			 r.setUnitImg_(null);
		});
		m.clear();
		m.put("total",total);
		m.put("rows",ls);
		return ResultUtil.ok(m);
	}
	
	
	
	
	public Object getUnitReviewDetail(Integer reviewId) {
		if(reviewId==null || reviewId <1)
			return ResultUtil.fail("查询失败");
		BuildingUnitReviewBean r = this.unitReviewMapper.getUnitDetail(reviewId);
		return ResultUtil.ok(r);
	}
	
}
